package com.moli.iov.down.remote.impl;

import com.moli.iov.down.constants.Constant;
import com.moli.iov.down.enums.OperateStatusEnum;
import com.moli.iov.down.enums.OperateTypeEnum;
import com.moli.iov.down.enums.OperatorResultEnum;
import com.moli.iov.down.model.dto.req.VehicleCommandQueryDto;
import com.moli.iov.down.model.dto.res.VehicleCommandResDto;
import com.moli.iov.down.model.po.VehicleCommand;
import com.moli.iov.down.remote.IVehicleCommandLogRemoteService;
import com.moli.iov.down.service.IVehicleCommandService;
import com.moli.iov.enums.UserTypeEnum;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ValidationUtils;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 命令日志业务
 * </p>
 *
 * @author zql
 * @since 2019/9/19 14:06
 */
@Slf4j
@Service(group = "${service.group}")
public class VehicleCommandLogRemoteServiceImpl implements IVehicleCommandLogRemoteService {

  @Autowired
  private IVehicleCommandService vehicleCommandService;

  /**
   * 命令日志查询
   * @param vehicleCommandQueryDto
   * @return
   */
  @Override
  public RestResponse<PageExtend<VehicleCommandResDto>> listVehicleCommand(
                          VehicleCommandQueryDto vehicleCommandQueryDto) {

    listVehicleCommandValid(vehicleCommandQueryDto);

    PageExtend<VehicleCommand> vehicleCommandPageExtend = vehicleCommandService
        .listVehicleCommand(vehicleCommandQueryDto);

    return RestResponse.success(convert(vehicleCommandPageExtend));
  }

  /**
   * 参数校验
   * @param vehicleCommandQueryDto
   */
  private void listVehicleCommandValid(VehicleCommandQueryDto vehicleCommandQueryDto){
    ValidationUtils.restResponseValidate(vehicleCommandQueryDto, VehicleCommandQueryDto.Base.class);

    if(vehicleCommandQueryDto.getSysUserType() == UserTypeEnum.COMPANY.getFlag() &&
        vehicleCommandQueryDto.getSysCompanyId() == null){
      throw new BusinessException("操作员企业信息异常");
    }

    if(vehicleCommandQueryDto.getSysUserType() == UserTypeEnum.COMPANY.getFlag()){
      vehicleCommandQueryDto.setCompanyId(vehicleCommandQueryDto.getSysCompanyId());
    }
  }

  /**
   * 转换成 PageExtend<VehicleCommandResDto>
   * @param page
   * @return
   */
  private PageExtend<VehicleCommandResDto> convert(PageExtend<VehicleCommand> page){
    PageExtend<VehicleCommandResDto> pageExtend = new PageExtend<>();
    pageExtend.setTotal(page.getTotal());
    pageExtend.setPageSize(page.getPageSize());
    pageExtend.setPageNumber(page.getPageNumber());

    List<VehicleCommand> vehicleCommands = page.getRecords();
    List<VehicleCommandResDto> vehicleCommandResDtos = new ArrayList<>();
    if(vehicleCommands != null){
      vehicleCommands.forEach(vc -> {
        VehicleCommandResDto vehicleCommandResDto = new VehicleCommandResDto();
        CommonUtils.copyProperties(vc, vehicleCommandResDto);
        vehicleCommandResDto.setStatusStr(OperateStatusEnum.getMsg(vc.getStatus()));
        vehicleCommandResDto.setResultStr(OperatorResultEnum.getMsg(vc.getResult()));

        String resultMemo = vehicleCommandResDto.getResultMemo();
        if(vc.getResult() == OperatorResultEnum.SUCCESS.getFlag() &&
            vehicleCommandResDto.getOperateType() == OperateTypeEnum.CHANGE_IP_PORT.getFlag()&&
          vc.getIpAndPortExtend() != null){
          resultMemo = Constant.CHANGE_PORT_MSG.replaceAll("@1", vc.getIpAndPortExtend().getOldIP());
          resultMemo = resultMemo.replaceAll("@2", String.valueOf(vc.getIpAndPortExtend().getOldPort()));
          resultMemo = resultMemo.replaceAll("@3", vc.getIpAndPortExtend().getIp());
          resultMemo = resultMemo.replaceAll("@4", String.valueOf(vc.getIpAndPortExtend().getPort()));
        }

        if(vc.getResult() == OperatorResultEnum.SUCCESS.getFlag() &&
            vehicleCommandResDto.getOperateType() == OperateTypeEnum.FREQUENCY.getFlag()&&
            vc.getFrequencyExtend() != null){
          resultMemo = Constant.FREQUENCY_MSG.replaceAll("@1", String.valueOf(vc.getFrequencyExtend().getOldFrequency()));
          resultMemo = resultMemo.replaceAll("@2", String.valueOf(vc.getFrequencyExtend().getFrequency()));
        }

        if(vc.getResult() == OperatorResultEnum.SUCCESS.getFlag() &&
            (vehicleCommandResDto.getOperateType() == OperateTypeEnum.UPGRADE.getFlag()||
                vehicleCommandResDto.getOperateType() == OperateTypeEnum.RESUME_FACTORY.getFlag())&&
            vc.getVersionExtend() != null){
          resultMemo = Constant.RESUME_FACTORY_MSG.replaceAll("@1", vc.getVersionExtend().getOldVersion());
          resultMemo = resultMemo.replaceAll("@2", vc.getVersionExtend().getVersion());
        }

        vehicleCommandResDto.setResultMemo(resultMemo);
        vehicleCommandResDtos.add(vehicleCommandResDto);
      });
    }

    pageExtend.setRecords(vehicleCommandResDtos);
    return pageExtend;
  }
}
